package com.zretc.controller;

import com.zretc.entity.Account;
import com.zretc.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/account")
public class AccountController {

    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @RequestMapping("/login")
    @ResponseBody
    public boolean login(@RequestParam(value = "accountName") String accountName,
                         @RequestParam(value = "password") String password,
                         HttpSession session){

        Account account = accountService.login(accountName, password);
        if (account==null){
            return false;
        }else{
            session.setAttribute("account",account);
        }
        return true;

    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ModelAndView register(@RequestParam(value = "accountName") String accountName,
                            @RequestParam(value = "password") String password,
                            @RequestParam(value = "") MultipartFile facePicture
                            ){
        logger.info("=========");
        String newFileName=null;
        if (facePicture.getSize()==0){
            //上传文件为空
            logger.info("=========文件为空=========");
        }else{
            //获取文件名字
            String filename = facePicture.getOriginalFilename();
            //获取文件的后缀名
            String laststring = filename.substring(filename.lastIndexOf("."));
            //用时间戳自定义文件名字
            String date=System.currentTimeMillis()+"";
            //文件新名字：时间戳+后缀名
            newFileName=date+laststring;
            //上传到本地文件夹中
            File f=new File("C:/image/"+newFileName);
            try {

                facePicture.transferTo(f);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Account account=new Account();
        account.setAccountName(accountName);
        account.setPassword(password);
        account.setFacePicture(newFileName);
        boolean b = accountService.register(account);
        ModelAndView mav = new ModelAndView();
        if (b==true){
            //注册成功
            mav.setViewName("redirect:/front/login.jsp");
            return mav;
        }
        mav.setViewName("redirect:/front/register.jsp");
        return mav;

    }

    @RequestMapping("/isExist")
    @ResponseBody
    public boolean isExist(@RequestParam(value = "accountName") String accountName){

        Account account = accountService.isExistByName(accountName);
        logger.info("===="+account);
        if (account==null){
            return false;
        }
        return true;

    }
     @RequestMapping("/getAccountInfo")
     @ResponseBody
     public Account getAccountInfo(@RequestParam(value = "accountName") String accountName){

        Account accountByName = accountService.getAccountByName(accountName);
        logger.info("====" + accountByName );
         return accountByName;
     }

/*    @RequestMapping("/updatePassword")
    @ResponseBody
    public Integer update(@RequestParam(value = "accountName") String accountName,
                          @RequestParam(value = "password") String password){
        int i = accountService.updataPasswordByAccountName(accountName,password);
        logger.info("====" + i);
        return i;

    }*/

    @RequestMapping(value = "/updatePassword", method = RequestMethod.GET)
    @ResponseBody
    public String updatePassword(@RequestParam(value = "accountName",required = false) String accountName,
                                 @RequestParam(value = "newPassword",required = false) String newPassword){
        int i = accountService.updataPasswordByAccountName(accountName, newPassword);
        return ""+i;

    }

    @RequestMapping("/outLogin")
    public String outLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("account");
        return "redirect:../front/login.jsp";
    }

}
